#include"../header.h"

int main(int argc, char const *argv[])
{
    if(argc<2)
    {
        perror("./a.out+prot");
        return -1;
    }
    int sid=socket(AF_INET,SOCK_DGRAM,0);
    printf("sid=%d\n",sid);

    int value=0;
    int size=sizeof(value);
    getsockopt(sid,SOL_SOCKET,SO_BROADCAST,&value,&size);
    value=1;
    setsockopt(sid,SOL_SOCKET,SO_BROADCAST,&value,size);
    getsockopt(sid,SOL_SOCKET,SO_BROADCAST,&value,&size);
    printf("value=%d\n",value);

    struct sockaddr_in saddr={0};
    saddr.sin_addr.s_addr=INADDR_ANY;
    saddr.sin_family=AF_INET;
    saddr.sin_port=htons(atoi(argv[1]));
    if(bind(sid,(struct sockaddr*)&saddr,sizeof(saddr))<0)
    { 
        perror("bind error\n");
        return -1;
    }    
    printf("绑定成功\n");
    char bufs[128]="";
    int len=0;
    while (1)
    {
        bzero(bufs,sizeof(bufs));
        len=recvfrom(sid,bufs,sizeof(bufs),0,NULL,NULL);
        printf("接收的数据是:%s数据长度为:%d\n",bufs,len);
        if(!strncmp(bufs,"quit",4))
            {
                printf("客户端退出......\n");
                break;
            }
    }
    close(sid);
    return 0;
}

